package com.bizihang.controller;

import com.bizihang.dto.User;
import com.fasterxml.jackson.annotation.JsonView;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import sun.net.www.protocol.http.AuthenticationInfo;

import java.util.Arrays;
import java.util.List;

/**
 * @author 毕子航 951755883@qq.com
 * @date 2018/08/14
 */
@RequestMapping("/users")
@RestController
public class UserController {

	@GetMapping()
	@JsonView(User.UserSimpleView.class)
	public List<User> get(@RequestParam String username) {
		System.out.println(ReflectionToStringBuilder.toString(username));
		List<User> users = Arrays.asList(new User("1", "1"), new User("2", "2"), new User("3", "3"));
		return users;
	}

	@GetMapping("/me")
	public Authentication currentUser(Authentication authentication) {
		return authentication;
	}

	@GetMapping("/me2")
	public UserDetails currentUser2(@AuthenticationPrincipal UserDetails userDetails) {
		return userDetails;
	}


	@GetMapping("/{userId:\\d+}")
	@JsonView(User.UserDetailView.class)
	public User getOne(@PathVariable String userId) {
		return new User("Tom", userId);
	}

	@PostMapping()
	public User create(@RequestBody User user) {
		System.out.println(user.getUsername());
		System.out.println(user.getCtime());
		user.setId(1);
		return user;
	}
}
